package m.system.task;

import m.common.model.HostInfo;
import m.common.service.HostInfoService;
import m.system.RuntimeData;
import m.system.SystemTaskRun;
import m.system.util.DateUtil;
import m.system.util.FileUtil;
import m.system.util.SystemUtil;

import java.io.File;
import java.io.PrintStream;
import java.util.Date;

public class SystemTask extends SystemTaskRun {
	public static void setLogPrint(){

		if(RuntimeData.getOutLog()) {
			try {
				HostInfo hi=HostInfoService.getCurrentHost();
				SystemUtil.println(hi);
				String d=hi.getIpport().split(":")[0]+"_"+hi.getServerPort();
				if(d.indexOf("/")!=0) d="";
				SystemUtil.printlnIf(true,"输出日志：",RuntimeData.getClassPath(),"log"+d+"/out_",DateUtil.format(new Date(),"yyyy-MM-dd HH-mm"),".log");
				File outFile=FileUtil.getFile(RuntimeData.getClassPath()+"log"+d+"/out_"+DateUtil.format(new Date(),"yyyy-MM-dd HH-mm")+".log");
				System.setOut(new PrintStream(outFile));
				File errFile=FileUtil.getFile(RuntimeData.getClassPath()+"log"+d+"/err_"+DateUtil.format(new Date(),"yyyy-MM-dd HH-mm")+".log");
				System.setErr(new PrintStream(errFile));
			}catch(Exception e) {
				SystemUtil.printlnIf(true,"设置日志输出失败!",e.getMessage());
			}
		}else{
			SystemUtil.printlnIf(true,"输出日志：控制台");
		}
	}
	@Override
	public void run(boolean isMain) {
		if(RuntimeData.getOutLog()) {
			try {
				HostInfo hi=HostInfoService.getCurrentHost();
				SystemUtil.println(hi);
				String d=hi.getIpport().split(":")[0]+"_"+hi.getServerPort();
				if(d.indexOf("/")!=0) d="";
				File outFile=FileUtil.getFile(RuntimeData.getClassPath()+"log"+d+"/out_"+DateUtil.format(new Date(),"yyyy-MM-dd HH-mm")+".log");
		        System.setOut(new PrintStream(outFile));
				File errFile=FileUtil.getFile(RuntimeData.getClassPath()+"log"+d+"/err_"+DateUtil.format(new Date(),"yyyy-MM-dd HH-mm")+".log");
				System.setErr(new PrintStream(errFile));
			}catch(Exception e) {
				SystemUtil.printlnIf(true,"设置日志输出失败!",e.getMessage());
			}
		}
	}

}
